<?php
class Globals
{
	public function connect(&$objDb)
	{	
		$objConf 	= new Zend_Config_Ini(ROOT_DIR . '/configs/config.ini', 'db');
		$arrConf 	= $objConf->db->toArray() + array('driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;'));
		$objDb 		= Zend_Db::factory($arrConf['adapter'], $arrConf['config']);
		Zend_Registry::set('objDb', $objDb);
	}	
	static function thumbImage($strFileName, $strFileOriginImages, $strFolderImagesThumb, $intSize=160)
	{
		$strThumbFile 		= $strFolderImagesThumb . $strFileName;
		$objThumb 			= new Vfriend_Thumbnail($strFileOriginImages, $strThumbFile);
		if ($objThumb->error_msg == '') {
			$objThumb->quality 		  			= 100;
			$objThumb->output_format 			= 'JPG,JPEG,jpg,jpeg,GIF,gif';
			$objThumb->jpeg_progressive 		= 1;
			$objThumb->bicubic_resample 		= false;
			$objThumb->allow_enlarge			= false;			
			$objThumb->size_width($intSize);
			$objThumb->process();
			$objThumb->save($strThumbFile);
		}
		
	}
	public function uploadAvatar($file, &$strPath='')
	{
		if ($file['name'] != '') {
			$strFolder			= date('Y/m/d/');
			$strFullFolder 		= DIR_AVATAR_FULL . $strFolder;
			$strThumbFolder 	= DIR_AVATAR_THUMB . $strFolder;
			$strSmallFolder 	= DIR_AVATAR_SMALL . $strFolder;
			$arrUploadResult 	= Vfriend_Upload::process($file, $strFullFolder, IMAGE_SIZE*1024*1024);			
			if ($arrUploadResult['error'] == 0) {
				$strPath		= $strFolder . $arrUploadResult['name'];
				$strFullPath 	= $arrUploadResult['fullpath'];								
				$strFileName	= $arrUploadResult['name'];
				$strThumbPath	= $strThumbFolder . $strFileName;
				$strSmallPath	= $strSmallFolder . $strFileName;
				$objThumb 		= new Vfriend_Thumbnail($strFullPath);
				if ($arrUploadResult['width'] > AVATAR_IMAGE_FULL_WIDTH) {
					$objThumb->quality 		  	= 100;
					$objThumb->output_format 	= 'JPG,JPEG,jpg,jpeg,GIF,gif';
					$objThumb->jpeg_progressive = 1;
					$objThumb->bicubic_resample = false;
					$objThumb->allow_enlarge	= false;
					$objThumb->size_width(AVATAR_IMAGE_FULL_WIDTH);
					$objThumb->process();
					$objThumb->save($strFullPath);					
				}				
				Vfriend_Function::mkDirectory($strThumbFolder);		
				Vfriend_Function::mkDirectory($strSmallFolder);		
				Vfriend_Crop::thumbSquare($strFullPath, $strThumbPath, AVATAR_IMAGE_THUMB_WIDTH, AVATAR_IMAGE_THUMB_HEIGHT);
				Vfriend_Crop::thumbSquare($strFullPath, $strSmallPath, AVATAR_IMAGE_SMALL_WIDTH, AVATAR_IMAGE_SMALL_HEIGHT);
			}
			return $arrUploadResult;
		}
		return null;
	}	
	public function uploadAlbum($file, &$strPath='', $intSize=640, $intThumbSize = 200) {
		if ($file['name'] != '') {
			$strFolder			= date('Y/m/d/');
			$strFullFolder 		= DIR_ALBUM_FULL . $strFolder;
			$strThumbFolder 	= DIR_ALBUM_THUMB . $strFolder;
			$arrUploadResult 	= Vfriend_Upload::process($file, $strFullFolder, IMAGE_SIZE*1024*1024);			
			if ($arrUploadResult['error'] == 0) {
				$strPath		= $strFolder . $arrUploadResult['name'];
				$strFullPath 	= $arrUploadResult['fullpath'];								
				$strFileName	= $arrUploadResult['name'];
				$strThumbPath	= $strThumbFolder . $strFileName;
				$objThumb 		= new Vfriend_Thumbnail($strFullPath);
				if ($arrUploadResult['width'] > $intSize) {
					$objThumb->quality 		  	= 100;
					$objThumb->output_format 	= 'JPG,JPEG,jpg,jpeg,GIF,gif';
					$objThumb->jpeg_progressive = 1;
					$objThumb->bicubic_resample = false;
					$objThumb->allow_enlarge	= false;
					$objThumb->size_width($intSize);
					$objThumb->process();
					$objThumb->save($strFullPath);					
				}	
				Globals::thumbImage($strFileName, $strFullPath, $strThumbFolder, $intThumbSize);
			}
			return $arrUploadResult;
		}
		return null;
	}
	public function uploadBlog($file, $intSize = 640,$intSmallThumb=50)
	{
		if ($file['name'] != '') {
			$strFolder			= date('Y/m/d/');
			$strFullFolder 		= DIR_BLOG_FULL . $strFolder;
			$strThumbFolder 	= DIR_BLOG_THUMB . $strFolder;
			$strSmallFolder 	= DIR_BLOG_SMALL . $strFolder;
			$arrUploadResult 	= Vfriend_Upload::process($file, $strFullFolder, IMAGE_SIZE*1024*1024);			
			if ($arrUploadResult['error'] == 0) {
				$arrUploadResult['path']	= $strFolder . $arrUploadResult['name'];
				$strFullPath 				= $arrUploadResult['fullpath'];				
				$strFileName				= $arrUploadResult['name'];
				$strThumbPath				= $strThumbFolder . $strFileName;
				$strSmallPath				= $strSmallFolder . $strFileName;
				$objThumb 					= new Vfriend_Thumbnail($strFullPath);				
				if ($arrUploadResult['width'] > $intSize) {
					$objThumb->quality 		  	= 100;
					$objThumb->output_format 	= 'JPG,JPEG,jpg,jpeg,GIF,gif';
					$objThumb->jpeg_progressive = 1;
					$objThumb->bicubic_resample = false;
					$objThumb->allow_enlarge	= false;
					$objThumb->size_width($intSize);
					$objThumb->process();
					$objThumb->save($strFullPath);					
				}	
				Globals::thumbImage($strFileName, $strFullPath, $strThumbFolder);
				Vfriend_Function::mkDirectory($strSmallFolder);				
				Vfriend_Crop::thumbSquare($strFullPath, $strSmallPath, $intSmallThumb, $intSmallThumb);
			}
			return $arrUploadResult;
		}
		return null;
	}
    public function uploadNews($file, $intSize = 640,$intSmallThumb=80)
	{
		if ($file['name'] != '') {
			$strFolder			= date('Y/m/d/');
			$strFullFolder 		= DIR_NEWS_FULL . $strFolder;
			$strThumbFolder 	= DIR_NEWS_THUMB . $strFolder;			
			$strSmallFolder 	= DIR_NEWS_SMALL . $strFolder;			
			$arrUploadResult 	= Vfriend_Upload::process($file, $strFullFolder, IMAGE_SIZE*1024*1024);			
			if ($arrUploadResult['error'] == 0) {
				$arrUploadResult['path']	= $strFolder . $arrUploadResult['name'];
				$strFullPath 				= $arrUploadResult['fullpath'];				
				$strFileName				= $arrUploadResult['name'];
				$strThumbPath				= $strThumbFolder . $strFileName;
				$strSmallPath				= $strSmallFolder . $strFileName;
				$objThumb 					= new Vfriend_Thumbnail($strFullPath);				
				if ($arrUploadResult['width'] > $intSize) {
					$objThumb->quality 		  	= 100;
					$objThumb->output_format 	= 'JPG,JPEG,jpg,jpeg,GIF,gif';
					$objThumb->jpeg_progressive = 1;
					$objThumb->bicubic_resample = false;
					$objThumb->allow_enlarge	= false;
					$objThumb->size_width($intSize);
					$objThumb->process();
					$objThumb->save($strFullPath);					
				}	
				Vfriend_Function::mkDirectory($strThumbFolder);
				Vfriend_Function::mkDirectory($strSmallFolder);				
				Vfriend_Crop::thumbSquare($strFullPath, $strThumbPath, 200, 110);
				Vfriend_Crop::thumbSquare($strFullPath, $strSmallPath, $intSmallThumb, $intSmallThumb);
			}
			return $arrUploadResult;
		}
		return null;
	}
	
	public function uploadPlaces($file, $intSize = 640,$intSmallThumb=130)
	{
		if ($file['name'] != '') {
			$strFolder			= date('Y/m/d/');
			$strFullFolder 		= DIR_PLACE_FULL . $strFolder;
			$strThumbFolder 	= DIR_PLACE_THUMB . $strFolder;			
			//$strSmallFolder 	= DIR_NEWS_SMALL . $strFolder;			
			$arrUploadResult 	= Vfriend_Upload::process($file, $strFullFolder, 3*1024*1024);			
			if ($arrUploadResult['error'] == 0) {
				$arrUploadResult['path']	= $strFolder . $arrUploadResult['name'];
				$strFullPath 				= $arrUploadResult['fullpath'];				
				$strFileName				= $arrUploadResult['name'];
				$strThumbPath				= $strThumbFolder . $strFileName;
				//$strSmallPath				= $strSmallFolder . $strFileName;
				$objThumb 					= new Vfriend_Thumbnail($strFullPath);				
				if ($arrUploadResult['width'] > $intSize) {
					$objThumb->quality 		  	= 100;
					$objThumb->output_format 	= 'JPG,JPEG,jpg,jpeg,GIF,gif';
					$objThumb->jpeg_progressive = 1;
					$objThumb->bicubic_resample = false;
					$objThumb->allow_enlarge	= false;
					$objThumb->size_width($intSize);
					$objThumb->process();
					$objThumb->save($strFullPath);					
				}	
				Vfriend_Function::mkDirectory($strThumbFolder);
				//Vfriend_Function::mkDirectory($strSmallFolder);				
				Vfriend_Crop::thumbSquare($strFullPath, $strThumbPath,PLACE_IMAGE_THUMB_WIDTH, PLACE_IMAGE_THUMB_HEIGHT);
				//Vfriend_Crop::thumbSquare($strFullPath, $strSmallPath, $intSmallThumb, $intSmallThumb);
			}
			return $arrUploadResult;
		}
		return null;
	}
}
?>
